package com.game.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.game.modal.PointDetail;
import com.game.modal.PointDetailListInfo;

@Mapper
public interface PointDetailMapper {
	
	@Insert("INSERT INTO point_detail (\n" +
			"	point_id,\n" +
			"	point,\n" +
			"	receiver_id,\n" +
			"	creat_time\n" +
			")\n" +
			"VALUES\n" +
			"	(#{pointId}, #{point}, #{receiverId}, now())")
	int insert(PointDetail detail); 
	
	@Select("SELECT\n" +
			"	p.point point,\n" +
			"	u.nick_name nickName,\n" +
			"	u.avatar_url avatarUrl,\n" +
			"   DATE_FORMAT(p.creat_time,'%Y年%m月%d日  %H:%i:%s') createTime\n"+
			"FROM\n" +
			"	point_detail p\n" +
			"JOIN user u ON u.openid = p.receiver_id\n"
			+ "WHERE p.point_id=#{pointId}")
	List<PointDetailListInfo> list(String pointId);

	@Insert(
			"<script>" +
			"INSERT INTO point_detail (\n" +
			"	point_id,\n" +
			"	point,\n" +
			"	receiver_id,\n" +
			"	creat_time\n" +
			")\n" +
			"VALUES\n" +
			"<foreach collection=\"list\" item=\"detail\" separator=\",\">" +
			"	(#{detail.pointId}, #{detail.point}, #{detail.receiverId}, now())\n"+
			"</foreach>"+
			"</script>"
			)
	int insertList(List<PointDetail> details);
	
	@Select("<script>\n"+ 
			"SELECT\n" +
			"	pd.id\n" +
			"FROM\n" +
			"	point_detail pd\n" +
			"WHERE\n" +
			"	pd.point_id = #{pointId}\n" +
			"<if test=\"uid!=null and uid!=''\">\n"+ 
			"AND pd.receiver_id = #{uid}\n" +
			"</if>"+
			"LIMIT 1</script>")
	Integer selectPointDetailBy(@Param("pointId")String pointId,@Param("uid")String uid);

	@Update("UPDATE\n"
			+ "point_detail\n"
			+ "SET receiver_id=#{uid}\n"
			+ "WHERE id=#{id}")
	int updatePointDetail(@Param("id")int id, @Param("uid")String uid);
	

}
